<?php

namespace common\services\order;

use common\models\order\OrderRefundModel;
use api\models\user\User;
/**
 * Desc 订单管理服务类
 * @author WMX
 */
class OrderRefundCommonService
{

    // 获取订单列表
    public static function getUnionAll($condition = [], $page = 1, $page_size = 20)
    {
        $query = OrderRefundModel::find()->alias('o')->leftJoin('user u', 'u.id=o.user_id');
        list($offset, $limit) = \common\tools\Util::getLimit($page, $page_size);
        if ($condition) {
            $query->where($condition);
        }
        $query->select(['o.*','u.phone as user_phone']);
        $data['total'] = intval($query->count());
        $query->offset($offset)->limit($limit);
        $data['list'] = $query->asArray()->all();
       
        foreach ($data['list'] as &$value) {
            $value['state_name'] = OrderRefundModel::$state[$value['state']] ?? '';
            $value['type_name'] = OrderRefundModel::$type[$value['type']] ?? '';
            $value['goods'] = json_decode($value['goods']);
        }

        return $data;
    }

    // 获取订单详情
    public static function getOrderDetail($refund_sn)
    {
        $order_info = OrderRefundModel::getDataOne(['refund_sn' => $refund_sn]);
        $user_info = User::getDataOne(['id'=> $order_info['user_id']]);
        $order_info['user_phone'] =  $user_info['phone'];
        $order_info['goods'] = json_decode($order_info['goods']);
        $order_info['state_name'] = OrderRefundModel::$state[$order_info['state']] ?? '';
        $order_info['type_name'] = OrderRefundModel::$type[$order_info['type']] ?? '';
        return $order_info;
    }
}
